﻿using HeerCards.Info;
using LitJson;
using Native.Tool.Http;
using PropertyChanged;
using System.ComponentModel;
using System.Text;

namespace HeerCards.Info
{
    [AddINotifyPropertyChangedInterface]
    public class VIPInfo : INotifyPropertyChanged
    {
        public bool isVIP { get; set; }
        public string VIPName { get; set; }
        public string Code { get; set; }
        public int Coin { get; set; }
        public string QQ { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public VIPInfo()
        {
            isVIP = false;
            VIPName = "";
        }
    }
}

namespace HeerCards.Helper
{
    public class VIPHelper
    {
        /// <summary>
        /// 检查授权码+qq的授权情况
        /// </summary>
        /// <param name="qq"></param>
        /// <param name="code"></param>
        /// <returns></returns>
        public static VIPInfo checkVIP(string code)
        {
            var info = new VIPInfo();
            var url = $"http://47.96.166.30/customcard/checkinfo.php?code={code}";

            var json = Encoding.Default.GetString(HttpWebClient.Get(url));
            JsonData jd = JsonMapper.ToObject(json);
            if (jd["err_code"].ToString() != "0")
            {
                info.isVIP = false;
                info.VIPName = "普通用户";
            }
            else
            {
                info.isVIP = true;
                info.VIPName = jd["data"]["Name"].ToString();
                info.Code = jd["data"]["Code"].ToString();
                info.Coin = int.Parse(jd["data"]["Coin"].ToString());
                info.QQ = jd["data"]["QQ"].ToString();

            }

            return info;
        }

        public static VIPInfo checkVIP(string qq, string code)
        {
            var info = new VIPInfo() { isVIP = false, VIPName = "" };
            var url = $"http://47.96.166.30/customcard/checkinfo.php?code={code}";

            var json = Encoding.Default.GetString(HttpWebClient.Get(url));
            JsonData jd = JsonMapper.ToObject(json);

            if (jd["err_code"].ToString() != "0")
            {
                info.isVIP = false;
                info.VIPName = "普通用户";
            }
            else
            {
                if (jd["data"]["QQ"].ToString() == qq)
                {
                    info.isVIP = true;
                    info.VIPName = jd["data"]["Name"].ToString();
                    info.Code = jd["data"]["Code"].ToString();

                    info.Coin = int.Parse( jd["data"]["Coin"].ToString());
                }
            }

            return info;
        }

        /// <summary>
        /// 替换指定授权码的授权qq
        /// </summary>
        /// <param name="code"></param>
        /// <param name="qq"></param>
        /// <returns></returns>
        public static bool bindVIPQQ(string code, string qq)
        {
            var url = $"http://47.96.166.30/customcard/bindcode.php?code={code}&qq={qq}";
            var json = Encoding.Default.GetString(HttpWebClient.Get(url));
            JsonData jd = JsonMapper.ToObject(json);
            if (jd["err_code"].ToString() != "0")
            {
                //错误
                return false;
            }
            else
            {
                //正确
                return true;
            }
        }
    }
}